package com.itheima.health.service.impl;

import com.itheima.health.dao.UserDao;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.User;
import com.itheima.health.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * @author zhangmeng
 * @description 用户服务实现类
 * @date 2019/9/6
 **/
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

//    @Override
//    public Integer add(User user) {
//        user.setPassword(passwordEncoder.encode(user.getPassword()));
//        Integer insert = userDao.insert(user);
//
//        return insert;
//    }

    @Override
    public User findByUsername(String username) {
        //调用DAO查询数据
        User user = userDao.selectByUsername(username);
        Set<Role> roles = userDao.selectByRoles(user.getId());
        for (Role role : roles) {
            Set<Permission> permissions = userDao.selectByP(role.getId());
            role.setPermissions(permissions);
            user.setRoles(roles);
            break;
        }
        return user;
    }
}
